package com.zqx.strategy.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.sql.SQLException;

public class ImageUtil {

    // 竖线的行数
    public static final int totalcol = 8;
    // 图片宽度
    public static final int imageWidth = 1850;
    // 行高
    public static final int rowheight = 40;
    // 起始高度
    public static final int startHeight = 10;
    // 起始宽度
    public static final int startWidth = 10;
    /*
     *     向表格中画内容
     */
    public BufferedImage drawImage(String title, int totalrow) throws SQLException {

        // 图片高度
        int imageHeight = totalrow * rowheight + 50;
        // 单元格宽度
        int colwidth = (int) ((imageWidth - 20) / totalcol);

        // 1.获取数据
        BufferedImage bufferedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);

        Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();

        graphics2D.setColor(Color.WHITE);
        graphics2D.fillRect(0,0, imageWidth, imageHeight);
        graphics2D.setColor(new Color(220,240,240));
        for (int j = 0; j < totalrow; j++) {
            graphics2D.setColor(Color.black);
            graphics2D.drawLine(startWidth, startHeight + (j + 1) * rowheight, startWidth + colwidth * totalcol,
                    startHeight + (j + 1) * rowheight);
        }
        // 画竖线
        for (int k = 0; k < totalcol + 1; k++) {
            graphics2D.setColor(Color.black);
            graphics2D.drawLine(startWidth + k * colwidth, startHeight + rowheight, startWidth + k * colwidth,
                    startHeight + rowheight * totalrow);
        }
        // 设置字体
        Font font = new Font("微软雅黑", Font.BOLD, 16);
        graphics2D.setFont(font);
        graphics2D.setColor(Color.RED);
        // 写标题
        graphics2D.drawString(title, startWidth, startHeight + rowheight - 10);
        return bufferedImage;
    }
}
